package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.enums.CommonStatusEnum;
import com.parking.mgr.useraccount.facede.UserServiceFacede;
import com.parking.mgr.useraccount.model.entity.UserInfoEntity;
import com.parking.mgr.useraccount.model.entity.UserPhoneEntity;
import com.parking.mgr.useraccount.model.mapper.UserPhoneMapper;
import com.parking.mgr.useraccount.model.param.UserPhoneOtherParam;
import com.parking.mgr.useraccount.model.param.UserPhoneParam;
import com.parking.mgr.useraccount.service.LoginTokenService;
import com.parking.mgr.useraccount.service.UserPhoneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserPhoneService
 * @Description 用户手机号service
 * @Auther: zhijianpu
 * @Date: 2023/5/25
 */
@Service
public class UserPhoneServiceImpl extends ServiceImpl<UserPhoneMapper, UserPhoneEntity> implements UserPhoneService {

    @Resource
    private UserServiceFacede userServiceFacede;


    @Resource
    private UserPhoneMapper userPhoneMapper;

    @Resource
    private LoginTokenService loginTokenService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSelfUserPhone(UserPhoneParam userPhoneParam, HttpServletRequest request) {

        this.saveUserPhone(loginTokenService.getPassportIdByToken(request),
                userPhoneParam.getFPhoneCode(), userPhoneParam.getFMobilePhoneNumber());
    }

    @Override
    public void addOtherUserPhone(UserPhoneOtherParam userPhoneOtherParam, HttpServletRequest request) {
        UserInfoEntity userInfoEntity = userServiceFacede.getUserInfoById(userPhoneOtherParam.getId());

        this.saveUserPhone(userInfoEntity.getFkUserPassportId(),
                userPhoneOtherParam.getFPhoneCode(), userPhoneOtherParam.getFMobilePhoneNumber());
    }


    @Override
    public void deleteUserPhoneByPassportId(String passportId) {
        userPhoneMapper.delete(new LambdaQueryWrapper<UserPhoneEntity>()
                .eq(UserPhoneEntity::getFkUserPassportId, passportId));
    }

    @Override
    public void editUserPhone(UserPhoneParam userPhoneParam, HttpServletRequest request) {
        this.editUserPhone(loginTokenService.getPassportIdByToken(request),
                userPhoneParam.getFPhoneCode(), userPhoneParam.getFMobilePhoneNumber());
    }

    @Override
    public void editOtherUserPhone(UserPhoneOtherParam userPhoneOtherParam, HttpServletRequest request) {
        UserInfoEntity userInfoEntity = userServiceFacede.getUserInfoById(userPhoneOtherParam.getId());

        this.editUserPhone(userInfoEntity.getFkUserPassportId(),
                userPhoneOtherParam.getFPhoneCode(), userPhoneOtherParam.getFMobilePhoneNumber());
    }

    private UserPhoneEntity getUserPhoneEntity(String passportId) {
        return userPhoneMapper.selectOne(new LambdaQueryWrapper<UserPhoneEntity>()
                .eq(UserPhoneEntity::getFkUserPassportId, passportId)
                .eq(UserPhoneEntity::getFEnableState, CommonStatusEnum.ENABLE.getCode()));

    }

    @Override
    public void saveUserPhone(String passportId, String phoneCode, String phoneNumber) {

        UserPhoneEntity userPhoneEntity = this.getUserPhoneEntity(passportId);
        if (ObjectUtil.isNotNull(userPhoneEntity)) {
            userPhoneEntity.setFPhoneCode(phoneCode);
            userPhoneEntity.setFMobilePhoneNumber(phoneNumber);

            this.updateById(userPhoneEntity);
            userServiceFacede.updateUserPhone(userPhoneEntity);
        } else {
            UserPhoneEntity newUserPhoneEntity = new UserPhoneEntity();
            newUserPhoneEntity.setFkUserPassportId(passportId);
            newUserPhoneEntity.setFPhoneCode(phoneCode);
            newUserPhoneEntity.setFMobilePhoneNumber(phoneNumber);
            this.save(newUserPhoneEntity);
            userServiceFacede.updateUserPhone(newUserPhoneEntity);
        }



    }

    @Override
    public UserPhoneEntity getUserInfoByPhone(String phoneNumber) {
        LambdaQueryWrapper<UserPhoneEntity> queryWrapper = new LambdaQueryWrapper<UserPhoneEntity>()
                .eq(UserPhoneEntity::getFMobilePhoneNumber, phoneNumber);

        return this.getOne(queryWrapper);
    }

    private void editUserPhone(String passportId, String phoneCode, String phoneNumber) {
        UserPhoneEntity userPhoneEntity = this.getUserPhoneEntity(passportId);

        userPhoneEntity.setFPhoneCode(phoneCode);
        userPhoneEntity.setFMobilePhoneNumber(phoneNumber);

        this.updateById(userPhoneEntity);
        userServiceFacede.updateUserPhone(userPhoneEntity);
    }

}
